const state = {
    isLogin: localStorage.getItem('isLogin'),
    userName: localStorage.getItem('userName'),
    avatar: localStorage.getItem('avatar'),
}

const mutations = {
    IS_LOGIN: (state, value) => {
        state.isLogin = value;
        localStorage.setItem('isLogin', value)
    },
    SET_USERNAME: (state, value) => {
        state.userName = value;
        localStorage.setItem('userName', value)
    },
    RETURN_LOGIN: () => {
        localStorage.removeItem('isLogin')
        localStorage.removeItem('userName')
        localStorage.removeItem('avatar')
        localStorage.removeItem('aboutList')
    },
    SET_AVATAR: (state, value) => {
        state.avatar = value;
        localStorage.setItem('avatar', value)
    }
};

const actions = {
    login({commit}) {
        return new Promise((resolve) => {
            setTimeout(() => {
                commit('IS_LOGIN', 1)
                resolve(1)
            }, 1000)
        })
    },
    setUserName({commit}, userName) {
        commit('SET_USERNAME', userName);
    },
    returnLogin({commit}) {
        return new Promise((resolve) => {
            setTimeout(() => {
                commit('RETURN_LOGIN')
                resolve('200')
            }, 1000);
        })
    },
    setAvatar({commit}, file) {
        return new Promise((resolve) => {
            setTimeout(() => {
                commit('SET_AVATAR', file);
                resolve('200');
            }, 1000);
        })
    }
}

const getters = {
    isLogin: (state) => state.isLogin,
    userName: (state) => state.userName,
    avatar: (state) => state.avatar
};

export default {
    namespaced: true,
    state,
    mutations,
    actions,
    getters
};

